######################################################################
# Strategic campaign attention to abortion before and after Dobbs
# Proceedings of the National Academy of Sciences
# Mellissa Meisels (correspondence: mellissa.meisels@yale.edu)
######################################################################
library(tidyverse)
library(scales)
library(ggpubr)
library(fixest)
library(broom)

PNAS_dat <- readRDS("Meisels_PNAS.rds")

######################################################################
# FIGURE 1 
######################################################################
attn_pct <- PNAS_dat %>% group_by(year, cand_pid, abortion) %>% summarize(wp_rate = mean(word_presence)) %>% ungroup() %>%
  ggplot(aes(x = year, y = wp_rate, color = cand_pid, linetype = abortion)) + geom_point() + geom_line() + 
  labs(x=NULL, y = "Campaigned on Issue", title = "(A) Campaign Attention by Party and Issue") + theme_classic() + 
  geom_vline(xintercept = 2022, linetype = "dashed") +
  scale_y_continuous(labels = percent_format(accuracy = 1)) + 
  guides(color = guide_legend(label.position = "bottom"), linetype = guide_legend(label.position = "bottom")) +
  annotate(geom="text", x=2020.9, y=.7, label="Dobbs Decision") +
  theme(axis.text = element_text(color = "black"), plot.title = element_text(hjust = .5, face = "bold"), legend.position = "left", legend.title=element_blank()) +
  scale_color_manual(values=c("blue", "red"), labels=c(" Democrats ","Republicans")) + 
  scale_linetype_manual(values = c("solid","dotted"), breaks=c("1","0"), labels=c("  Abortion  ","Other Issues"))

said_abortion <- PNAS_dat %>% filter(word_presence==1 & abortion==1) %>%
  group_by(cand_pid, year) %>% summarize(rate_said_abortion = mean(said_abortion)) %>% ungroup() %>%
  ggplot(aes(x = year, y = rate_said_abortion, color = cand_pid)) + geom_point() + geom_line() + theme_classic() +
  geom_vline(xintercept = 2022, linetype = "dashed") +
  scale_y_continuous(labels = percent_format(accuracy = 1), breaks = c(seq(.4,.65, by=.05))) + 
  scale_color_manual(values=c("blue", "red"), guide="none") +
  theme(axis.text = element_text(color = "black"), plot.title = element_text(hjust = .5, face = "bold")) +
  labs(x=NULL, y="Abortion Stances Using Term Verbatim", title = "(B) Usage of Term \'\'Abortion\'\' by Party") +
  annotate("text", x=2020.9, y=.67, label="Dobbs Decision")

ggarrange(attn_pct, said_abortion, nrow = 1, common.legend = T, legend = "right")

######################################################################
# FIGURE 2 
######################################################################
lm_abort_attn_D <- feols(word_presence ~ year_2022 + year_2024, PNAS_dat, subset = ~cand_pid=="D" & abortion==1, cluster = ~cand_id) %>%
  tidy(conf.int = TRUE) %>% filter(term!="(Intercept)") %>% mutate(party = "D", issue = "abortion")

lm_other_attn_D <- feols(word_presence ~ year_2022 + year_2024, PNAS_dat, subset = ~cand_pid=="D" & abortion==0, cluster = ~cand_id) %>%
  tidy(conf.int = TRUE) %>% filter(term!="(Intercept)") %>% mutate(party = "D", issue = "other")

lm_abort_attn_R <- feols(word_presence ~ year_2022 + year_2024, PNAS_dat, subset = ~cand_pid=="R" & abortion==1, cluster = ~cand_id) %>%
  tidy(conf.int = TRUE) %>% filter(term!="(Intercept)") %>% mutate(party = "R", issue = "abortion")

lm_other_attn_R <- feols(word_presence ~ year_2022 + year_2024, PNAS_dat, subset = ~cand_pid=="R" & abortion==0, cluster = ~cand_id) %>%
  tidy(conf.int = TRUE) %>% filter(term!="(Intercept)") %>% mutate(party = "R", issue = "other")

dat_attn_party <- rbind(lm_abort_attn_D, lm_other_attn_D, lm_abort_attn_R, lm_other_attn_R)

attn_party <- dat_attn_party %>%
  ggplot(aes(x = estimate, y = term, alpha = issue, shape=issue, color = party)) + 
  geom_point(position = position_dodge(width=0.5)) + geom_vline(xintercept = 0, linetype = "dashed") +
  geom_errorbarh(aes(xmin=conf.low, xmax=conf.high), position = position_dodge(width=.5), height=0) +
  scale_color_manual(values=c("blue", "red"), guide="none") + 
  scale_alpha_manual(values=c(1, .5), labels=c("Abortion","Other Issues")) +
  scale_shape_manual(values=c("circle", "triangle"), labels=c("Abortion","Other Issues")) +
  guides(alpha = guide_legend(label.position = "bottom"), shape = guide_legend(label.position = "bottom")) +
  scale_x_continuous() + scale_y_discrete(labels = c("2022", "2024")) +
  theme_classic() + labs(x = "Estimated Change in Issue Attention\n", y= NULL, title = "(A) By Issue and Party") + 
  theme(axis.text = element_text(color = "black"), plot.title = element_text(hjust = .5, face = "bold"), legend.title=element_blank()) + 
  coord_flip() + 
  annotate("text", y=1.05, x=.15, color="blue", label="Democrats") +
  annotate("text", y=1.2, x=.07, color="red", label="Republicans")

lm_ban_abortion <- feols(word_presence ~ cand_pid*year_2022 + cand_pid*year_2024, PNAS_dat, subset = ~pre_roe_ban==1 & abortion==1, cluster = ~cand_id) %>%
  tidy(conf.int = TRUE) %>% filter(term %in% c("cand_pidR:year_2022", "cand_pidR:year_2024")) %>% mutate(pre_roe = "ban", issue = "abortion")

lm_ban_other <- feols(word_presence ~ cand_pid*year_2022 + cand_pid*year_2024, PNAS_dat, subset = ~pre_roe_ban==1 & abortion==0, cluster = ~cand_id) %>%
  tidy(conf.int = TRUE) %>% filter(term %in% c("cand_pidR:year_2022", "cand_pidR:year_2024")) %>% mutate(pre_roe = "ban", issue = "other")

lm_noban_abortion <- feols(word_presence ~ cand_pid*year_2022 + cand_pid*year_2024, PNAS_dat, subset = ~pre_roe_ban==0 & abortion==1, cluster = ~cand_id) %>%
  tidy(conf.int = TRUE) %>% filter(term %in% c("cand_pidR:year_2022", "cand_pidR:year_2024")) %>% mutate(pre_roe = "noban", issue = "abortion")

lm_noban_other <- feols(word_presence ~ cand_pid*year_2022 + cand_pid*year_2024, PNAS_dat, subset = ~pre_roe_ban==0 & abortion==0, cluster = ~cand_id) %>%
  tidy(conf.int = TRUE) %>% filter(term %in% c("cand_pidR:year_2022", "cand_pidR:year_2024")) %>% mutate(pre_roe = "noban", issue = "other")

dat_ban_diff <- rbind(lm_ban_abortion, lm_ban_other, lm_noban_abortion, lm_noban_other)

ban_diff <- dat_ban_diff %>%
  ggplot(aes(x = estimate, y = term, alpha = issue, shape=issue, color = pre_roe)) + 
  geom_point(position = position_dodge(width=0.5)) + geom_vline(xintercept = 0, linetype = "dashed") +
  geom_errorbarh(aes(xmin=conf.low, xmax=conf.high), position = position_dodge(width=.5), height=0) +
  scale_color_manual(values=c("forestgreen", "purple"), guide="none") + scale_alpha_manual(values=c(1, .5), guide="none") +
  scale_x_continuous() + scale_y_discrete(labels = c("2022", "2024")) + scale_shape(guide="none") +
  theme_classic() + labs(x = "Estimated Change in Partisan\nDifference in Issue Attention", y= NULL, title = "(B) Partisan Difference by Issue and State Law") + 
  theme(axis.text = element_text(color = "black"), plot.title = element_text(hjust = .5, face = "bold")) + 
  coord_flip() +
  annotate("text", y=.8, x=-.4, color="forestgreen", label="Immediate") +
  annotate("text", y=.8, x=-.44, color="forestgreen", label="Post-Roe Ban") +
  annotate("text", y=.7, x=.1, color="purple", label="No") +
  annotate("text", y=.7, x=.07, color="purple", label="Immediate") +
  annotate("text", y=.7, x=.03, color="purple", label="Change")

ggarrange(attn_party, ban_diff, nrow = 1, common.legend = T, legend = "right")









